<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Model\Cart;
use App\Model\Commodity_info;
use Illuminate\Http\Request;

/**
 * Class CartController
 * @package App\Http\Controllers\Api
 * @User wuqin
 * @Date 2018/4/1 0001
 * @Time 15:11
 */
class CartController extends Controller
{
    /**
     * @var Cart
     */
    private $cartModel;
    /**
     * @var User_cart
     */
    private $infoModel;

    /**
     * UserCartController constructor.
     * @param Cart $cart
     * @param User_cart $info
     */
    function __construct(Cart $cart, Commodity_info $info)
    {
        $this->cartModel = $cart;
        $this->infoModel = $info;
    }

    public function delete_cart(Request $request)
    {
        $post = $request->input();
        if (empty($post) || count($post) == 0) {
            return return_array(1, "非法操作");
        }
        unset($post["_token"]);
        $this->cartModel = $this->cartModel->delete_cart_by_ids($post);
        if (empty($this->cartModel))
            return return_message(1, "删除失败");

        return return_message(0, "删除成功");
    }
    /**
     * 方法 : add_cart
     * @User wuqin
     * @DateTime 2018-04-01 15:11
     * @param Request $request
     * @return array
     */
    public function add_cart(Request $request)
    {
        $post = $request->input();
        if (empty($post) || count($post) == 0) {
            return return_array(1, "非法操作");
        }
        if (
            empty($post["id"]) ||
            empty($post["info"]) ||
            empty($post["color"]) ||
            empty($post["size"]) ||
            empty($post["number"])
        )
            return return_array(1, "非法参数");
        $key = "commodity_info:" . $post["info"] . ":commodity:" . $post["id"] . ":color:" . $post["color"] . ":size:" . $post["size"];
        $infoData = $this->infoModel->get_redis($key);
        if (empty($infoData) || count($infoData) == 0)
            return return_array(1, "该商品不存在");

        $user = get_session_user();
        if (empty($user))
            return return_array(1, "非法用户");

        $data = array(
            "user_id" => $user["user_id"],
            "commodity_id" => $infoData["commodity_id"],
            "info_id" => $infoData["commodity_info_id"],
            "color_id" => $infoData["commodity_color_id"],
            "size_id" => $infoData["commodity_size_id"],
            "number" => $post["number"],
            "unit_price" => $infoData["commodity_info_price"],
            "all_price" => bcmul($post["number"], $infoData["commodity_info_price"]),
        );
        $where = array(
            array("where", "user_id", "=", $user["user_id"]),
            array("where", "info_id", "=", $infoData["commodity_info_id"]),
        );
        $this->cartModel->trans_begin();
        $res = $this->cartModel->get($where);
        if (empty($res) || count($res) == 0)
            $this->cartModel = $this->cartModel->add_cart($data);
        else {
            $res = $res[0];
            $cartModel = $this->cartModel->find($res["cart_id"]);
            $data["number"] += $res["number"];
            $data["all_price"] = bcmul($data["number"], $infoData["commodity_info_price"]);
            $this->cartModel = $cartModel->update_cart($data);
        }
        if (empty($this->cartModel)) {
            $this->cartModel->trans_rollback();
            return return_array(1, "添加失败");
        }
        $this->cartModel->trans_commit();
        session(array("user.cart.num" => bcadd(session("user.cart.num"), 1)));
        return return_array(0, "加入购物车成功", $this->cartModel->toArray());
    }

    /**
     * 方法 : up_cart_num
     * @User wuqin
     * @DateTime 2018-04-01 15:11
     * @param Request $request
     * @return array
     */
    public function up_cart_num(Request $request)
    {
        $post = $request->input();
        if (empty($post) || count($post) == 0) {
            return return_array(1, "非法操作");
        }
        if (
            empty($post["commodity"]) ||
            empty($post["info"]) ||
            empty($post["color"]) ||
            empty($post["size"]) ||
            empty($post["number"])
        )
            return return_array(1, "非法参数");
        $key = "commodity_info:" . $post["info"] . ":commodity:" . $post["commodity"] . ":color:" . $post["color"] . ":size:" . $post["size"];
        $infoData = $this->infoModel->get_redis($key);
        if (empty($infoData) || count($infoData) == 0)
            return return_array(1, "该商品不存在");

        $user = get_session_user();
        if (empty($user))
            return return_array(1, "非法用户");

        $data = array(
            "user_id" => $user["user_id"],
            "commodity_id" => $infoData["commodity_id"],
            "info_id" => $infoData["commodity_info_id"],
            "color_id" => $infoData["commodity_color_id"],
            "size_id" => $infoData["commodity_size_id"],
            "number" => $post["number"],
            "unit_price" => $infoData["commodity_info_price"],
            "all_price" => bcmul($post["number"], $infoData["commodity_info_price"]),
        );
        $cartModel = $this->cartModel->find($post["cart"]);
        $this->cartModel = $cartModel->update_cart($data);
        if (empty($this->cartModel)) {
            return return_array(1, "更新失败");
        }
        return return_array(0, "更新成功");
    }

    /**
     * 方法 : set_cart_session
     * @User wuqin
     * @DateTime 2018-04-01 16:58
     * @param Request $request
     * @return array
     */
    public function set_cart_session(Request $request)
    {
        $post = $request->input();
        if (empty($post) || count($post) == 0) {
            return return_array(1, "非法操作");
        }
        unset($post["_token"]);
        session(array("user.cart.info" => $post));
        return return_array(0, "OK");
    }
    /**
     * 方法 : del_cart
     * @User wuqin
     * @DateTime 2018-04-01 15:11
     * @param Request $request
     * @return array
     */
    public function del_cart(Request $request)
    {
        $post = $request->input();
        if (empty($post) || count($post) == 0) {
            return return_array(1, "非法操作");
        }
    }

    /**
     * 方法 : reduce_cart_num
     * @User wuqin
     * @DateTime 2018-04-01 15:11
     * @param Request $request
     * @return array
     */
    public function reduce_cart_num(Request $request)
    {
        $post = $request->input();
        if (empty($post) || count($post) == 0) {
            return return_array(1, "非法操作");
        }
    }
}
